package com.own.component.common.base.entity;

import com.own.component.common.base.entity.po.BaseBean;
import com.own.component.common.base.entity.po.BasePo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;

import java.io.Serializable;

/**
 * BaseBo
 *
 * @author chenxueli
 * @date 2020/6/15 11:06
 */
@Schema(title = "业务层处理对象bo")
@Getter
@Setter
@ToString
@FieldNameConstants
public abstract class BaseBo<T extends BaseBean> implements Serializable {

    @Schema(title = "备注")
    private String memo;

    /**
     * 初始化
     */
    public void init() {

    }

    /**
     * 验证数据
     */
    public void check() {

    }

    /**
     * 转持久化对象
     *
     * @return 持久化对象
     */
    public T toPo() {
        return toPo(null);
    }

    /**
     * 转持久化对象
     *
     * @param entity 原持久化对象
     * @return 持久化对象
     */
    public T toPo(T entity) {
        check();
        if (entity == null) {
            return null;
        }
        if (entity instanceof BasePo) {
            ((BasePo) entity).setMemo(memo);
        }
        return entity;
    }

}
